package com.myway.core.common;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;



/**
 * 淘淘商城自定义响应结构
 */
public class JsonUtils {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 将对象转换成json字符串。
     * <p>Title: pojoToJson</p>
     * <p>Description: </p>
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
    	try {
			String string = MAPPER.writeValueAsString(data);
			return string;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    /**
     * 将json结果集转化为对象
     * 
     * @param jsonData json数据
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            T t = MAPPER.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
    	try {
    		List<T> list = MAPPER.readValue(jsonData, javaType);
    		return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	return null;
    }

    public static String jsonToHtml(String json) {
        StringBuffer result = new StringBuffer();
        int length = json.length();
        int number = 0;
        char key = 0;
        char flag = 0;
        for (int i = 0; i < length; i++) {
            key = json.charAt(i);
            if(key == ':') flag = json.charAt(i+1);
            if(key == '{') {
                result.append(key);
                result.append("<br/>");
                number++;
                result.append(indent(number));
                continue;
            }
            if(key == '}') {
                result.append("<br/>");
                number--;
                result.append(indent(number));
                result.append(key);
                continue;
            }
            if((key == ',') && !((json.charAt(i - 1) == '}') && (json.charAt(i + 1) == '{'))){
                if((flag == '[' && (json.charAt(i - 1) == ']')) || (flag == '"' && (json.charAt(i - 1) == '"')) || (flag != '[' && flag != '"')){
                    result.append(key);
                    result.append("<br/>");
                    result.append(indent(number));
                    continue;
                }else if(i>=3 && ((json.charAt(i - 1) == '}') || (json.charAt(i - 2) == '}') || (json.charAt(i - 3) == '}'))){
                    result.append(key);
                    result.append("<br/>");
                    result.append(indent(number));
                    continue;
                }
            }
            result.append(key);
        }
        return result.toString();
    }

    private static String indent(int number) {
        StringBuffer result = new StringBuffer();
        for(int i = 0; i < number; i++) {
            result.append("&#12288;");
        }
        return result.toString();
    }
}
